//
//  WoDeShangPinViewController.m
//  EJLX
//
//  Created by adc on 15/11/12.
//  Copyright © 2015年 江苏一键联信息科技有限公司. All rights reserved.
//

#import "WoDeShangPinViewController.h"
#import "WoDeShangPinTableViewCell.h"
#import "HttpsManager.h"
#import "SecondShangPinGuanLi.h"

#import "SearchTableViewCell.h"
#import "SearchViewController.h"

@interface WoDeShangPinViewController ()

@end

@implementation WoDeShangPinViewController
-(void)viewWillAppear:(BOOL)animated
{
     [self request];
}
- (void)viewDidLoad {
    [super viewDidLoad];
   
        UIView*view=[[UIView alloc]initWithFrame:CGRectMake(0,20,WIDE, 44)];
    view.backgroundColor=RGB_COLOR(0x00c49f);
    [self.view addSubview:view];
    UIButton*backBut=[[UIButton alloc]initWithFrame:CGRectMake(10,7, 30,30)];
    [backBut setImage:[UIImage imageNamed:@"返回箭头"] forState:UIControlStateNormal];
    [backBut addTarget:self action:@selector(backButClick) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:backBut];
    UIButton*shaiXuan=[[UIButton alloc]initWithFrame:CGRectMake(WIDE-50, 10,40, 24)];
    [shaiXuan setTitle:@"筛选" forState:UIControlStateNormal];
    shaiXuan.font=[UIFont systemFontOfSize:12];
    [shaiXuan setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [shaiXuan addTarget:self action:@selector(shaiXuanBut) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:shaiXuan];
   // for (int i=0; i<3; i++)
    //{
    
    //}
    UIView*butView=[[UIView alloc]initWithFrame:CGRectMake(0,HEIGHT-56, WIDE, 56)];
    butView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:butView];
    for (int i=0; i<3; i++)
    {
        UIButton*lastBut=[[UIButton alloc]initWithFrame:CGRectMake((WIDE-60)/6+((WIDE-60)/3+20)*i,5,20,20)];
        NSArray*ARY=[NSArray arrayWithObjects:@"新增商品",@"商品管理",@"分享店铺", nil];
        [lastBut setBackgroundImage:[UIImage imageNamed:ARY[i]] forState:UIControlStateNormal];
        lastBut.tag=i;
        [lastBut addTarget:self action:@selector(lastBut:) forControlEvents:UIControlEventTouchUpInside];
        [butView addSubview:lastBut];
        UILabel*lastLable=[[UILabel alloc]initWithFrame:CGRectMake((WIDE-180)/6+((WIDE-180)/3+60)*i,30,60,20)];
        NSArray*ary=[NSArray arrayWithObjects:@"新增商品",@"批量管理",@"分享微店", nil];
        lastLable.text=ary[i];
        lastLable.font=[UIFont systemFontOfSize:12];
        lastLable.textAlignment=NSTextAlignmentCenter;
        lastLable.textColor=[UIColor blackColor];
        [butView addSubview:lastLable];
    }
    UILabel*label=[[UILabel alloc]initWithFrame:CGRectMake(WIDE/2-50, 10,100, 24)];
    label.text=@"我的商品";
    label.textAlignment=NSTextAlignmentCenter;
    label.textColor=[UIColor whiteColor];
    [view addSubview:label];
    
    UILabel*label1=[[UILabel alloc]initWithFrame:CGRectMake(0, 0,40, 20)];
    label1.text=@"商品";
    label1.textColor=[UIColor grayColor];
    
   // [_textField addTarget:self action:@selector(textFieldBut) forControlEvents:UIControlEventTouchUpInside];
        UIButton*textFieldBut=[[UIButton alloc]initWithFrame:CGRectMake(0,0,WIDE,44)];
    
        [textFieldBut addTarget:self action:@selector(textFieldBut) forControlEvents:UIControlEventTouchUpInside];
    [_search addSubview:textFieldBut];
    
    
    UIButton*but1=[[UIButton alloc]initWithFrame:CGRectMake(0, 0,60, 20)];
    [but1 setTitle:@"搜索" forState:UIControlStateNormal];
    [but1 setBackgroundColor:RGB_COLOR(0x00c49f)];
    [but1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [but1 addTarget:self action:@selector(butClick) forControlEvents:UIControlEventTouchUpInside];
    
    _tableView.rowHeight=170;
    [_tableView registerNib:[UINib nibWithNibName:@"WoDeShangPinTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
        // Do any additional setup after loading the view from its nib.
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 2;
}
- (void)requestEnd{
    [_tableView.pullToRefreshView stopAnimating];
    
    [_tableView.infiniteScrollingView stopAnimating];
    
}
-(void)request

{
    
    [HttpsManager registerWoDeShangPin:_shopId type:@"1" completion:^(int errcode, ShangPinGuanLi *guanLi)
     {
         if (guanLi!=nil)
         {
             _guanLi=guanLi;
             _dataAry=_guanLi.listAry;
             __weak WoDeShangPinViewController *weakSelf = self;
             [_tableView addPullToRefreshWithActionHandler:^{
                 NSLog(@"当进行下拉刷新执行该方法");
                 [weakSelf performSelector:@selector(requestEnd) withObject:nil afterDelay:1.0f];
             }];
             
             [_tableView addInfiniteScrollingWithActionHandler:^{
                 [weakSelf performSelector:@selector(requestEnd) withObject:nil afterDelay:1.0f];
             }];
             [_tableView reloadData];
         }
         else
         {
             if (errcode==ERRCODE_SHOPNOT)
             {
                 SHOW_ALERT(@"您的店铺不存在");
                 [self.navigationController popViewControllerAnimated:YES];
             }
             else
             {
                 LoginVC*vc=[[LoginVC alloc]init];
                 [self.navigationController pushViewController:vc animated:YES];

             }
                      }

     }];
//    [HttpsManager registerWoDeShangPin:_shopId type: completion:^(ShangPinGuanLi *guanLi)
//     {
//         
//         
//     }];

}
-(void)shaiXuanBut
{
    searchView=[[UIView alloc]initWithFrame:CGRectMake(WIDE-200, 64, 200, 264+44)];
    searchView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:searchView];
    searchTable=[[UITableView alloc]initWithFrame:CGRectMake(0,44,200, 264) style:UITableViewStylePlain];
    searchTable.delegate=self;
    searchTable.dataSource=self;
    [searchView addSubview:searchTable];
    [searchTable registerNib:[UINib nibWithNibName:@"SearchTableViewCell" bundle:nil] forCellReuseIdentifier:@"celll"];
    UIButton*guanBiBut=[[UIButton alloc]initWithFrame:CGRectMake(130,0,33,33)];
    [guanBiBut setBackgroundImage:[UIImage imageNamed:@"关闭"] forState:UIControlStateNormal];
    [guanBiBut addTarget:self action:@selector(guanbiBut) forControlEvents:UIControlEventTouchUpInside];
    [searchView addSubview:guanBiBut];
}
-(void)guanbiBut
{
    [searchView removeFromSuperview];
}
//返回按钮
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView==searchTable)
    {
        return 6;
    }
   
        return _dataAry.count;
    
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString*typeStr=[NSString stringWithFormat:@"%ld",indexPath.row+1];
    [HttpsManager registerWoDeShangPin:_shopId type:typeStr completion:^(int errcode, ShangPinGuanLi *guanLi)
     {
         if (guanLi!=nil)
         {
             _guanLi=guanLi;
             _dataAry=_guanLi.listAry;
             [_tableView reloadData];
         }
         else
         {
             if (errcode==ERRCODE_SHOPNOT)
             {
                 SHOW_ALERT(@"您的店铺不存在");
                 [self.navigationController popViewControllerAnimated:YES];
             }
             else
             {
                 LoginVC*vc=[[LoginVC alloc]init];
                 [self.navigationController pushViewController:vc animated:YES];
                 
             }
         }
         
     }];
    [searchView removeFromSuperview];

//    [HttpsManager registerWoDeShangPin:_shopId type:typeStr completion:^(ShangPinGuanLi *guanLi)
//    {
//        if (guanLi!=nil)
//        {
//            _guanLi=guanLi;
//            _dataAry=_guanLi.listAry;
//            [_tableView reloadData];
//        }
//        else
//        {
//            LoginVC*vc=[[LoginVC alloc]init];
//            [self.navigationController pushViewController:vc animated:YES];
//        }
//
//
//    }];
}
- (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(5_0)
{
    
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (tableView==searchTable)
    {
        SearchTableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"celll" forIndexPath:indexPath];
        NSArray*ary=[[NSArray alloc]initWithObjects:@"全部商品",@"品牌分销商品",@"自营商品",@"集市商品",@"已上架商品",@"已下架商品", nil];
        cell.label.text=ary[indexPath.row];
        return cell;
    }
   // else
   // {
        SecondShangPinGuanLi*guanLi=_dataAry[indexPath.row];
        WoDeShangPinTableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    [cell.picture sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",guanLi.imgUrl]] placeholderImage:[UIImage imageNamed:@"头像1"]];
     
        cell.pinPai.text=guanLi.title;
        if (guanLi.discountPrice)
        {
            cell.shouJia.text=[NSString stringWithFormat:@"%.2f",guanLi.discountPrice*0.01];
            
        }
        else
        {
            cell.shouJia.text=[NSString stringWithFormat:@"%.2f",guanLi.price*0.01];
        }
        cell.yongJin.text=[NSString stringWithFormat:@"%.2f",guanLi.commision*0.01];
        cell.kuCun.text=[NSString stringWithFormat:@"%d",guanLi.stockNum];
    cell.zhiDing.tag=indexPath.row;
    [cell.zhiDing addTarget:self action:@selector(zhiDingBut:) forControlEvents:UIControlEventTouchUpInside];
    
    
        return cell;
  //  }
    
   
}
-(void)lastBut:(UIButton*)sender
{
    switch (sender.tag)
    {
        case 0:
        {
            
        }
            break;
        case 1:
        {
            PiLiangGuanLiViewController*vc=[[PiLiangGuanLiViewController alloc]init];
            vc.shopId=_shopId;
            [self presentViewController:vc animated:YES completion:^{
                nil;
            }];
        }
            break;
        case 2:
        {
            
        
        }
            break;
        default:
            break;
    }
}
-(void)zhiDingBut:(UIButton*)sender
{
    SecondShangPinGuanLi*sec=_dataAry[sender.tag];
    NSString*productIdStr=[NSString stringWithFormat:@"%d",sec.productId];
    [HttpsManager registerzhiDing:productIdStr completion:^(NSString *errmsg)
    {
        SHOW_ALERT(errmsg);
       
    }];
     [self  request];
}
-(void)textFieldBut
{
    SearchViewController*vc=[[SearchViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}
//- (void)textFieldDidBeginEditing:(UITextField *)textField
//{
//    SearchViewController*vc=[[SearchViewController alloc]init];
//    [self.navigationController pushViewController:vc animated:YES];
//    
//}
-(void)backButClick
{
    [self.navigationController popViewControllerAnimated:YES];
}
//搜索按钮
-(void)butClick
{
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


@end
